package com.zrar.ak.gkpt.service.impl.manage;

import com.alibaba.druid.util.StringUtils;
import com.zrar.ak.gkpt.dao.IManageCodeInfoDao;
import com.zrar.ak.gkpt.model.bo.ManageCodeInfoBO;
import com.zrar.ak.gkpt.model.vo.UserVO;
import com.zrar.ak.gkpt.service.manage.IManageCodeInfoService;
import com.zrar.ak.gkpt.util.BusinessEnum;
import com.zrar.ak.gkpt.util.CommonTools;
import com.zrar.ak.gkpt.util.JsonResult;
import com.zrar.ak.gkpt.util.JsonResultEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName: ManageCodeInfoService
 * @Description 编码相关信息Service层
 * @Author liyang
 * @Time 2020/04/03
 */
@Service
@Slf4j   //该注解可以进行日志输出  log
public class ManageCodeInfoService implements IManageCodeInfoService {

    @Autowired
    private IManageCodeInfoDao manageCodeInfoDao;


    @Override
    public JsonResult cudCodeInfo(JsonResult jsonResult, String formDataJson, UserVO userVO) throws Exception{
        /**
         * 编码信息表数据的增删改
         * 1.传入的数据若没有id值,则为新增
         * 1.1 若传入的数据没有parentId,则为一级新增数据
         * 1.2 若传入的数据有parentId,则为非一级新增数据
         * 1.2.1 对应的parentId没有对应的数据，则报错,提示传入数据有误
         * 1.2.2 对应的parentId有对应的数据则为普通的新增
         * 2.传入的数据有id值则为修改
         * 2.1 有效性操作(isEffective) ----如果当前级别置为有效，则其上级递归编码都为有效，如果当前级别置为无效，则下级递归编码都为无效
         * 2.2 逻辑删除(isDelete字段置为 1 )------其下级递归编码也都执行删除操作
         * 2.3 普通修改
         */
        ManageCodeInfoBO manageCodeInfoBO = CommonTools.jsonStrToObject(formDataJson, ManageCodeInfoBO.class);
        String id = manageCodeInfoBO.getId();
        if(StringUtils.isEmpty(id)){
            //1.传入的数据若没有id值,则为新增
            //新增传入的值:  codeValue(必传),effective(可传可不传:若未传默认为有效)
            //              parentId(可传可不传:若传了，则添加子级,若未传，则默认为一级编码),
            //              orderNum(可传可不传:若传了，则使用实际传的值，若未传，则从数据库获取)
            //orderNum（排序）的值获取
            Integer orderNum = manageCodeInfoBO.getOrderNum();
            if(orderNum == null && StringUtils.isEmpty(manageCodeInfoBO.getParentId())){
                orderNum = manageCodeInfoDao.queryOneLevelNum();
                orderNum = orderNum + 1;
            }else if(orderNum == null && !StringUtils.isEmpty(manageCodeInfoBO.getParentId())){
                orderNum = manageCodeInfoDao.queryNumByParentId(manageCodeInfoBO.getParentId());
                orderNum = orderNum + 1;
            }
            manageCodeInfoBO.setId(CommonTools.getUUID());
            manageCodeInfoBO.setCreatorId(userVO.getUserId());
            manageCodeInfoBO.setCreateTime(new Date());
            manageCodeInfoBO.setUpdateUserId(userVO.getUserId());
            manageCodeInfoBO.setUpdateTime(manageCodeInfoBO.getCreateTime());
            manageCodeInfoBO.setIsDelete(BusinessEnum.IS_DELETE_N.getValue());
            manageCodeInfoBO.setOrderNum(orderNum);
            if(StringUtils.isEmpty(manageCodeInfoBO.getParentId())){
                //1.1 若传入的数据没有parentId,则为一级新增数据
                manageCodeInfoBO.setCodeLevel(1);
                manageCodeInfoBO.setCodeKey(getCodeKey(null,manageCodeInfoDao));
                if(StringUtils.isEmpty(manageCodeInfoBO.getIsEffective())){
                    //如果传入的有效性为null,一级新增数据默认为有效
                    manageCodeInfoBO.setIsEffective(BusinessEnum.IS_EFFECTIVE_Y.getValue());
                }
            }else{
                //1.2 若传入的数据有parentId,则为非一级新增数据
                ManageCodeInfoBO parentManageCodeInfo = manageCodeInfoDao.selectByPrimaryKey(manageCodeInfoBO.getParentId());
                if(parentManageCodeInfo == null){
                    //1.2.1 对应的parentId没有对应的数据，则报错,提示传入数据有误
                    log.error("传入的parentId在数据库中未查到对应表记录",new Throwable());
                }else{
                    //1.2.2 对应的parentId有对应的数据则为普通的新增
                    manageCodeInfoBO.setCodeLevel(parentManageCodeInfo.getCodeLevel()+1);
                    manageCodeInfoBO.setCodeKey(getCodeKey(parentManageCodeInfo,manageCodeInfoDao));
                    manageCodeInfoBO.setIsEffective(parentManageCodeInfo.getIsEffective());
                }
            }
            manageCodeInfoDao.insert(manageCodeInfoBO);
        }else{
            //2.传入的数据有id值则为修改
            //2.1 普通修改
            //2.2 逻辑删除(isDelete字段置为 1 )------其下级递归编码也都执行删除操作
            //2.3 有效性操作(effective) ----如果当前级别置为无效，则下级递归编码都为无效
            //                         -----如果当前级别置为有效，则其上级递归编码都为有效，
            //获取数据库还未做修改的数据
            ManageCodeInfoBO codeInfo = manageCodeInfoDao.selectByPrimaryKey(id);
            manageCodeInfoBO.setCreatorId(codeInfo.getCreatorId());
            manageCodeInfoBO.setCreateTime(codeInfo.getCreateTime());
            manageCodeInfoBO.setUpdateUserId(userVO.getUserId());
            manageCodeInfoBO.setUpdateTime(new Date());
            manageCodeInfoBO.setParentId(codeInfo.getParentId());
            manageCodeInfoBO.setCodeLevel(codeInfo.getCodeLevel());
            manageCodeInfoBO.setCodeKey(codeInfo.getCodeKey());
            if(manageCodeInfoBO.getOrderNum() == null){
                manageCodeInfoBO.setOrderNum(codeInfo.getOrderNum());
            }
            //codeValue(编码的编码值)由页面传入
            //2.1 有效性操作(isEffective) ----如果当前级别置为有效，则其上级递归编码都为有效，如果当前级别置为无效，则下级递归编码都为无效
            Map map =new HashMap();
            map.put("id",id);
            if(BusinessEnum.IS_EFFECTIVE_Y.getValue().equals(manageCodeInfoBO.getIsEffective())){
                map.put("isEffective",BusinessEnum.IS_EFFECTIVE_Y.getValue());
                //当前级别置为有效，则其上级递归编码都为有效
                manageCodeInfoDao.setUpCodeInfoIsEffective(map);
            }else if(BusinessEnum.IS_EFFECTIVE_N.getValue().equals(manageCodeInfoBO.getIsEffective())){
                //当前级别置为无效，则下级递归编码都为无效
                map.put("isEffective",BusinessEnum.IS_EFFECTIVE_N.getValue());
                manageCodeInfoDao.setLowCodeInfoIsEffective(map);
            }
            //2.2 逻辑删除(isDelete字段置为 1 )------其下级递归编码也都执行删除操作
            if(BusinessEnum.IS_DELETE_Y.getValue().equals(manageCodeInfoBO.getIsDelete())){
                map.put("isDelete",BusinessEnum.IS_DELETE_Y.getValue());
                manageCodeInfoDao.setLowCodeInfoIsDelete(map);
            }else{
                //若不执行删除操作，则默认不删除
                manageCodeInfoBO.setIsDelete(BusinessEnum.IS_DELETE_N.getValue());
            }
            //2.3 普通修改
            manageCodeInfoDao.updateByPrimaryKey(manageCodeInfoBO);
        }
        CommonTools.jsonResultSetParameter(jsonResult,JsonResultEnum.CODE_SUCCESS);
        return jsonResult;
    }


    /**
     * 传入父级编码信息获取编码信息表对应的codeKey值
     * @param parentManageCodeInfo
     * @param manageCodeInfoDao
     * @return
     */
    public static String getCodeKey(ManageCodeInfoBO parentManageCodeInfo, IManageCodeInfoDao manageCodeInfoDao){
        String codeKey = "";
        int num=-99;
        if(parentManageCodeInfo!=null && !StringUtils.isEmpty(parentManageCodeInfo.getId())){
            //非一级目录
            num = manageCodeInfoDao.queryNumByParentId(parentManageCodeInfo.getId());
        }else if(parentManageCodeInfo == null){
            //一级目录
            num = manageCodeInfoDao.queryOneLevelNum();
        }
        num = num + 1;
        if(num>0 && num<10){
            codeKey = "00" + num;
        }else if(num>=10 && num<100){
            codeKey = "0" + num;
        }else if(num>=100 && num<1000){
            codeKey = "" + num;
        }else if(num<=0){
            log.error("传入getCodeKey方法的参数有误", new Throwable());
        } else{
            log.error("一个父级编码下最多可存在999个子编码", new Throwable());
        }
        if(parentManageCodeInfo!=null && !StringUtils.isEmpty(parentManageCodeInfo.getId())){
            //非一级目录
            codeKey = parentManageCodeInfo.getCodeKey() + codeKey;
        }
        return codeKey;
    }
}
